import request from '@/utils/request'
import VueCookies from 'vue-cookies'
// import qs from 'qs'
//post使用data    get使用 params 
//get请求
export function query(url, method) {
    return request({
      url: url,
      method: method
    })
}

//post请求
export function postQuery(url, params) {
  return request({
    url: url,
    method: 'POST',
    data: params
  })
}

//params传参方式
export function postQuery2(url, params) {
  return request({
    url: url,
    method: 'POST',
    params: params
  })
}

export function getQuery(url, params) {
  return request({
    url: url,
    method: 'GET',
    params: params
  })
}

export const languageList = [{
  code: 'zh-CN',
  name: '简体中文' 
},{
  code: 'zh-TW',
  name: '繁體中文' 
},{
  code: 'en-US',
  name: 'english' 
},{
  code: 'jp-JP',
  name: '日本語' 
}];

//转换语言
export function changeLanguage(language) {
  VueCookies.set('language',language);
  let params = {
    language: language
  }
  return request({
    url: '/change/language',
    method: 'GET',
    params: params
  })
}

//获取语言
export function getLanguage() {

  return VueCookies.get('language') ? VueCookies.get('language') : languageList[2].code;
}

//上传图片
export function uploadImg(file) {
  var fd = new FormData()
	fd.append('file', file)
  return request({
    url: '/member/upload/image',
    method: 'POST',
    data: fd
  })
}

//批量图片上传
export function uploadImgs(files) {
  var fd = new FormData()
  for(let i = 0; i < files.length; i ++){
    fd.append('files', files[i])
  }
  return request({
    url: '/member/upload/images',
    method: 'POST',
    data: fd
  })
}

//上传视频
export function uploadvideo(file) {
  var fd = new FormData()
	fd.append('file', file)
  return request({
    url: '/member/upload/video',
    method: 'POST',
    data: fd
  })
}

const emotionList = ['zb1_1', 'zb1_2', 'zb1_3', 'zb1_4', 'zb1_5', 'zb1_6', 'zb1_7', 'zb1_8','zb1_9', 'zb1_10', 'zb1_11', 'zb1_12', 'zb1_13', 'zb1_14', 'zb1_15', 'zb1_16','zb1_17','zb1_18', 'zb1_19', 'zb1_20', 'zb1_21', 'zb1_22', 'zb1_23', 'zb1_24','zb1_25', 'zb1_26', 'zb1_27', 'zb1_28', 'zb1_29', 'zb1_30', 'zb1_31', 'zb1_32','zb1_33', 'zb1_34', 'zb1_35', 'zb1_36', 'zb1_37', 'zb1_38', 'zb1_39', 'zb1_40','zb1_41', 'zb1_42', 'zb1_43', 'zb1_44', 'zb1_45', 'zb1_46', 'zb1_47', 'zb1_48','zb1_49', 'zb1_50', 'zb1_51', 'zb1_52', 'zb1_53', 'zb1_54', 'zb1_55', 'zb1_56','zb1_57', 'zb1_58', 'zb1_59', 'zb1_60', 'zb1_61', 'zb1_62', 'zb1_63', 'zb1_64','zb1_65', 'zb1_66', 'zb1_67', 'zb1_68', 'zb1_69', 'zb1_70', 'zb1_71', 'zb1_72','zb1_73', 'zb1_74', 'zb1_75', 'zb1_76', 'zb1_77', 'zb1_78', 'zb1_79', 'zb1_80','zb1_81', 'zb1_82', 'zb1_83', 'zb1_84', 'zb1_85', 'zb1_86', 'zb1_87', 'zb1_88','zb1_89', 'zb1_90', 'zb1_91', 'zb1_92', 'zb1_93', 'zb1_94', 'zb1_95', 'zb1_96','zb1_97', 'zb1_98', 'zb1_99', 'zb1_100', 'zb1_101', 'zb1_102', 'zb1_103', 'zb1_104']

//根据表情文字  获取表情
export function getEmotionHtml(txt){
  txt += '';
  let html = ''
  // let html = `<div @click="touchMsg('${txt}')">`;
  // let html = `<div @touchstart="gotouchstart(${txt})" @touchmove="gotouchmove" @touchend="gotouchend">`;
  //加个控制，防止无限循环
  let i = 0;
  //查找这个txt
  while(txt.indexOf('#') != -1 || i > 100){
    let one = txt.indexOf('#');
    let two = txt.indexOf(';');
    let name = txt.substring(one + 1, two);
    let index = emotionList.indexOf(name);
    //加上前面的文本
    html += txt.substring(0, one); 
    //加上图片
    if(index > -1){
      html += `<img src="https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/${index}.gif">`
    }
    
    txt = txt.substring(two + 1, txt.length); 
    i ++;
  }
  //加上最后的文本
  html += txt;
  // html += '</div>';
  // let index = emotionList.indexOf(name);
  return html;
}

//金额格式化
export function formatMoney(num) {
　　num = parseFloat(num);
　　num = num.toFixed(2);
　　num = num.toLocaleString();
　　return num;
}

//时间戳格式化
export function formatDateTime(inputTime) {
  var date = new Date(inputTime);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};